---
title: 'Figure B2'
author: "Preuss, Reyes, Somerville, Wu"
date: "2/28/2025"
output:
  html_document:
    toc: true
    toc_depth: 3
    toc_float: true
    number_sections: true
editor_options: 
  chunk_output_type: console
---
# Virtual Environment

To convert a project to use renv, call `renv::init()`. It adds three new files and directories to your project:
```{r}
#install.packages('renv')
#renv::init()
```

# Load 
```{r}
library(readr)
d <- read_csv("./data/luck_questions.csv", col_names=TRUE)
```


# Variables
Any variables that need to created, transformed, etc.

## IDs
```{r}
d$userid <- as.character(d$userid)
head(d$userid)
```


## Order
`ql2ordercapture` codes the order in which `ql2r1`, `ql2r2`, $\ldots$, `ql2r7` appear to respondent. 
```{r}
d$ordercapture <- d$ql2ordercapture; head(d$ordercapture)

#remove special characters; e.g., '[', ',', and ']'
## leave space separators
d$ordercapture <- gsub('[^[:alnum:] ]', '', d$ordercapture)
head(d$ordercapture)
```

Create new vectors indicating the question at $q=1$, $\ldots$, $q=7$.
```{r}
library(tidyr)
#create order labels (for columns)
orders <- paste0("q", seq(1,7,1)); orders

#split ordercapture vector into 7 columns
d <- separate(data=d, col=ordercapture, into=orders, 
         sep=' ', remove=TRUE, convert=FALSE)
```

# Likert-Data
```{r}
library(reshape2)

#Column names of likert responses
r <- paste0("ql2r", seq(1,7,1)); r 

#melt data
dr <- melt(d[,c("userid",r)], id='userid')
str(dr$value)

#vector of factor format
dr$factor <- as.factor(dr$value)
str(dr$factor)
levels(dr$factor)

#vector of luck type description
dr$luck_type <- dr$variable
levels(dr$luck_type) <- c("The family or society that you are born into", #ql2r1
                           "Unforeseen events (e.g. financial crises or booms, natural disasters, good harvests)", #ql2r2
                           "Access to education (e.g. quality of public schools in district)", #ql2r3
                           "Unexpected windfalls (e.g. winning the lottery, inheritance)", #ql2r4
                           "Your social and professional network", #ql2r5
                           "Fortuitous encounters and coincidences (e.g. right place at the right time)", #ql2r6
                           "Genetic makeup or natural ability" #ql2r7
                          )

head(dr); tail(dr)
```

# Plot Likerts
```{r}
library(ggplot2)
library(RColorBrewer)
library(ggthemes)
```

## Prepare Data for Plotting
```{r}
library(dplyr)

responses <- as.factor(c(1,2,3,4,5))

df <- data.frame(luck_type=dr$luck_type, importance=dr$factor)
head(df)

df %>% 
  group_by(luck_type, importance) %>% 
  count() %>% 
  ungroup() %>% 
  group_by(luck_type) %>% 
  mutate(percent = 100*n / sum(n)) %>% 
  mutate(percent = round(percent, 0)) %>% 
  mutate(bar_text = paste0(percent, "%")) %>% 
  ungroup() -> df_for_plotting
head(df_for_plotting)

nna <- which(is.na(df_for_plotting$importance)==F)

df_for_plotting <- df_for_plotting[nna, ]
head(df_for_plotting)
df_for_plotting %>% 
  filter(importance == responses[5]) %>% 
  arrange(percent) %>% 
  pull(luck_type) -> order_y
head(order_y)

df_for_plotting %>% 
  mutate(luck_type = factor(luck_type, levels = order_y)) %>% 
  mutate(importance = factor(importance, levels = responses[5:1])) -> df_ordered
head(df_ordered)
```


```{r}
my_font <- "Roboto Condensed"
#my_font <- "Sans"
library(showtext)
font_add_google(name = my_font, family = my_font)
showtext_auto()

library(ggtext) # Ref: https://cran.r-project.org/web/packages/ggtext/index.html

# Prepare title + subtitle + caption: 

p_title <- "Important Luck Factors"
p_subtitle = "Circumstances beyond a person’s control—or, their luck—can impact their life earnings. Please indicate how important you think the following factors are for determining people’s life earnings."
p_caption <- "1= not important at all ... 5 = extremely important."

#theme_set(theme_minimal())
```

```{r}
gg1 <- ggplot(data=df_ordered, aes(y=luck_type, x=percent, fill=importance)) +
        geom_col(width=0.8, position="fill") + 
        theme(plot.margin = unit(rep(0.7, 4), "cm")) +  
        scale_fill_manual(values = c(`1` = "#e36c33", 
                                     `2` = "#edad88",
                                     `3` = "#c7cdd1",
                                     `4` = "#829cb2",
                                     `5` = "#3e6487"),
                          breaks = c("1","2","3","4","5")) +
        scale_x_continuous(expand = c(0, 0), 
                           labels = paste0(seq(0, 100, 25), "%")) + 
        scale_y_discrete(expand = c(0, 0)) + 
        labs(fill = p_caption) +
        theme(text = element_text(family = my_font)) + 
        theme(legend.position = "top", 
              #legend.justification='left',
              legend.box = "horizontal",
              legend.title = element_text(size = 16, 
                                         family = my_font, 
                                         color = "grey10"),
              legend.text = element_text(size = 16, 
                                         family = my_font, 
                                         color = "grey10"),
              legend.key.height = unit(0.35, "cm"),
              legend.key.width = unit(0.15*3, "cm"),
              legend.box.margin = margin(0,1,0,0,"cm")
              ) + 
        theme(axis.title = element_blank()) + 
        theme(panel.grid.minor = element_blank()) + 
        theme(panel.grid.major.x = element_line(color = "grey70", 
                                                size = 0.8)) + 
        theme(axis.text = element_text(color = "grey30", 
                                       size = 18, 
                                       family = my_font)) #+ 
        #theme(plot.title = element_markdown(size = 16, face = "bold")) + 
        #theme(plot.caption = element_text(size = 10.5, 
        #                                  color = "grey40", 
        #                                  vjust = -1.5, 
        #                                  hjust = 0)) + 
        #theme(plot.subtitle = element_text(size = 11.5, 
        #                                   color = "grey10")) + 
        #theme(plot.title.position = "plot") +  
        #theme(plot.caption.position = "plot")

# For displaying percent of "5": 

df_ordered %>% 
  filter(importance == "5") -> df_for_text1

# Ad text layers: 

gg1 <- gg1 + 
  geom_text(data = df_for_text1 %>% filter(percent > 2),
            aes(y = luck_type, x = 0.98, label = bar_text), 
            size = 6.5, color = "white", family = my_font, hjust = 1.2) 

gg1

ggsave(filename = "./output/lucklikert.png", plot=gg1,
       width=6.5, height=2.5, units="in", dpi=300)
```


